package com.stoken.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 检查用户是否登录的过滤器
 * 
 * @author Fred
 * 
 */
public class CheckLoginFilter implements Filter {
	/**
	 * 初始化控制文件
	 */
	protected FilterConfig filterConfig = null;
	private List<String> notCheckURLList = new ArrayList<String>(); 
	private List<String> checkURIForSecondPWD = new ArrayList<String>();
	private List<String> checkURLForThirdPWD = new ArrayList<String>();

	public void doFilter(ServletRequest servletRequest,
			ServletResponse servletResponse, FilterChain filterChain)
			throws IOException, ServletException {

		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		
		if (!checkLogin(request)) {
			response.sendRedirect(request.getContextPath() + "/login");
			return;
		}
		
		/**需要输入2级密码uri*/
		if(checkSecondPwd(request)){
			response.sendRedirect(request.getContextPath() + "/login/secondPwd");
			return;
		}
		
		/**需要输入3级密码判断URL*/
		if(checkThirdPwd(request)){
			response.sendRedirect(request.getContextPath() + "/login/thirdPwd");
			return;
		}
		filterChain.doFilter(servletRequest, servletResponse);
	}
	
	

	public void destroy() {
		notCheckURLList.clear();
	}
	
	private boolean checkSecondPwd(HttpServletRequest request) {
		 String uri = request.getServletPath() + (request.getPathInfo() == null ? "" : request.getPathInfo());   
		 for(String checkUrl : checkURIForSecondPWD){
			 if(uri.contains(checkUrl)){
				 Object secondKey = request.getSession().getAttribute(StokenUtils.ACCESS_SECOND_PWD);
				 if(secondKey != null){
					 return false;
				 }
				 request.getSession().setAttribute(StokenUtils.PARENT_URL, request.getContextPath()+uri+(request.getQueryString()==null ? "" : request.getQueryString()));
				 return true;
			 }
		 }
		 return false;	 
	}
	
	private boolean checkThirdPwd(HttpServletRequest request) {
		 String uri = request.getServletPath() + (request.getPathInfo() == null ? "" : request.getPathInfo());   
		 for(String checkUrl : checkURLForThirdPWD){
			 if(uri.contains(checkUrl)){
				 Object thirdKey = request.getSession().getAttribute(StokenUtils.ACCESS_THIRD_PWD);
				 if(thirdKey != null){
					 return false;
				 }
				 request.getSession().setAttribute(StokenUtils.PARENT_THIRD_URL, request.getContextPath()+uri+(request.getQueryString()==null ? "" : request.getQueryString()));
				 return true;
			 }
		 }
		 return false;	 
	}

	private boolean checkLogin(HttpServletRequest request) {
		 String uri = request.getServletPath() + (request.getPathInfo() == null ? "" : request.getPathInfo());   
		 for(String checkUrl : notCheckURLList){
			 if(uri.contains(checkUrl)){
				 return true;
			 }
		 }	 
		 Object loginKey = request.getSession().getAttribute(
				StokenUtils.ACCESS_ROLE);
		 if (loginKey != null) {
			if (loginKey.toString().equals(StokenUtils.ACCESS_ROLE) && !uri.contains(StokenUtils.ADMIN_ACCESS_ROLE) && !uri.contains(StokenUtils.SERVICE_PROVIDERS_ACCESS_ROLE)) {
				return true;
			}else if(loginKey.toString().equals(StokenUtils.ADMIN_ACCESS_ROLE) && uri.contains(StokenUtils.ADMIN_ACCESS_ROLE)){
				return true;
			}else if(loginKey.toString().equals(StokenUtils.SERVICE_PROVIDERS_ACCESS_ROLE) && uri.contains(StokenUtils.SERVICE_PROVIDERS_ACCESS_ROLE)){
				return true;
			}
			 
		 }
		 return false;

	}

	public void init(FilterConfig filterConfig) throws ServletException {
		this.filterConfig = filterConfig;
		String notCheckURLListStr = filterConfig
				.getInitParameter("exceptUrls");

		if (notCheckURLListStr != null) {
			StringTokenizer st = new StringTokenizer(notCheckURLListStr, ",");
			notCheckURLList.clear();
			while (st.hasMoreTokens()) {
				notCheckURLList.add(st.nextToken());
			}
		}
		
		//常规操作
		checkURIForSecondPWD.add("/mlMR/Index");
		checkURIForSecondPWD.add("/memberBI/myprofile");
		checkURIForSecondPWD.add("/tlMR/Index");
		
		//会员管理
		checkURIForSecondPWD.add("/memberBI/registerIndex");
		checkURIForSecondPWD.add("/memberBI/activeMemberIndex");
		checkURIForSecondPWD.add("/memberBI/saleIndex");
		checkURIForSecondPWD.add("/memberBI/sectorIndex");
		checkURIForSecondPWD.add("/memberBI/applyUpgradeIndex");
		checkURIForSecondPWD.add("/agents/index");
		
		//财务管理
		checkURLForThirdPWD.add("/financial/achievementList");
		checkURLForThirdPWD.add("/financial/withdrawalsList");
		checkURLForThirdPWD.add("/financial/transfer");
	}

}
